package org.bpms.service.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.bpms.model.admin.Smenu;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 后台菜单业务层
 * @author hupei
 *
 */
public class MenuService {
	
	/**
	 * 保存菜单
	 * @param menu
	 * @return
	 */
	@Before(Tx.class)
	public Smenu saveOrUpdate(Smenu menu){
		if (null != menu.getStr("id")){
			menu.update();
		}else{
			menu.set("id", UUID.randomUUID().toString().replaceAll("-", ""));
			menu.save();
		}
		return menu;
	}
	
	/**
	 * 递归删除菜单
	 * @param menu
	 */
	@Before(Tx.class)
	public void delMenu(Smenu menu){
		menu = Smenu.me.findById(menu.getStr("id"));
		if (null != menu){
			List<Smenu> slist = Smenu.me.find("select * from smenu where pid=?" ,
					menu.getStr("id"));
			for (Smenu sm:slist){
				this.delMenu(sm);
			}
			Smenu.me.deleteById(menu.getStr("id"));
			Db.update("delete from srolemenu where mid=?" , menu.getStr("id"));
		}
	}
	
	/**
	 * 递归获取角色所拥有的菜单
	 * @param sl
	 * @param roleId
	 * @param menu
	 */
	public void menuFromRole(List<Smenu> sl , String roleId , Smenu menu){
		List<Smenu> slist = null;
		if (null == menu.getStr("id")){
			slist = Smenu.me.find("select a.id , a.pid , a.text , a.seq , a.icon , a.url , case when b.rid is null "
					+ "then 'false' else 'true' end as checked from smenu a left join srolemenu "
					+ "b on a.id=b.mid and b.rid=? where a.pid='-1'" , 
					roleId);
		}else{
			slist = Smenu.me.find("select a.id , a.pid , a.text , a.seq , a.icon , a.url , case when b.rid is null "
					+ "then 'false' else 'true' end as checked from smenu a left join srolemenu "
					+ "b on a.id=b.mid and b.rid=? where a.pid=?" , 
					roleId , menu.getStr("id"));
		}
		menu.setSmenus(slist);
		if (null != menu.getStr("id") && menu.getStr("pid").equals("-1")){//只加入顶层的menu
			sl.add(menu);
		}
		if (null != slist && slist.size() > 0){
			for (Smenu temp : slist){
				this.menuFromRole(sl, roleId, temp);
			}
		}		
	}
	
	/**
	 * 递归查询用户菜单
	 * @param smenu
	 * @param userId
	 * @param menu
	 */
	public void allChildMenu(List<Smenu> sl , String userId , Smenu menu){
		List<Smenu> slist = null;
		if (null == menu.getStr("id")){
			slist = Smenu.me.find("select * from smenu where id in(select distinct(b.mid) "
					+ "from suserrole a inner join srolemenu b on a.rid=b.rid where a.rid "
					+ "in(select rid from suserrole where uid=?)) and pid='-1' order by seq asc" , 
					userId);
		}else{
			slist = Smenu.me.find("select * from smenu where id in(select distinct(b.mid) "
					+ "from suserrole a inner join srolemenu b on a.rid=b.rid where a.rid "
					+ "in(select rid from suserrole where uid=?)) and pid=? order by seq asc" , 
					userId , menu.getStr("id"));
		}
		menu.setSmenus(slist);
		if (null != menu.getStr("id") && menu.getStr("pid").equals("-1")){//只加入顶层的menu
			sl.add(menu);
		}
		if (null != slist && slist.size() > 0){
			for (Smenu temp : slist){
				this.allChildMenu(sl, userId, temp);
			}
		}
	}
	
	/**
	 * 递归查询所有菜单
	 * @param sl
	 * @param menu
	 */
	public void allMenu(List<Smenu> sl , Smenu menu){
		List<Smenu> slist = null;
		if (null == menu.getStr("id")){
			slist = Smenu.me.find("select * from smenu where  pid='-1' order by seq asc");
		}else{
			slist = Smenu.me.find("select * from smenu where pid=? order by seq asc" , menu.getStr("id"));
		}
		menu.setSmenus(slist);
		if (null != menu.getStr("id") && menu.getStr("pid").equals("-1")){//只加入顶层的menu
			sl.add(menu);
		}
		if (null != slist && slist.size() > 0){
			for (Smenu temp : slist){
				this.allMenu(sl , temp);
			}
		}		
	}
	
	/**
	 * 递归保存菜单树
	 */
	@Before(Tx.class)
	public void saveTree(Map maptree){
		if (null != maptree.get("pId")){
			Smenu menu = new Smenu();
			menu.set("pid", maptree.get("pId"))
			           .set("text", (String)maptree.get("text"))
			           .set("seq", (Integer)maptree.get("seq"))
			           .set("icon", (String)maptree.get("faicon"))
			           .set("url", (String)maptree.get("url"));
			if (null != maptree.get("id")){
				menu.set("id", maptree.get("id"));
				menu.update();
			}else{
				menu.set("id", UUID.randomUUID().toString().replaceAll("-", ""));
				menu.save();
			}
		}
		if (null != maptree.get("children")){
			List list = (ArrayList) maptree.get("children");
			if (null != list){
				for (int i = 0 ; i < list.size() ; i++){
					Map temp = (Map)list.get(i);
					if (null != temp){
						saveTree(temp);
					}
				}
			}
		}
	}
	
	/**
	 * 查找菜单
	 * @param menuId
	 * @return
	 */
	public Smenu findById(String menuId){
		Smenu menu = Smenu.me.findById(menuId);
		return menu;
	}
}
